package com.jzo2o.trade.strategy;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.jzo2o.api.trade.dto.response.PayResDTO;
import com.jzo2o.api.trade.enums.PayChannelEnum;
import com.jzo2o.api.trade.enums.PayTypeEnum;
import com.jzo2o.trade.config.WeChatPayProperties;
import com.jzo2o.trade.model.domain.Trading;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import static cn.hutool.core.img.ImgUtil.IMAGE_TYPE_PNG;
/**
 * 微信支付策略
 */
@Slf4j
@Service
public class WxPayHandler implements PayHandler {

    @Autowired
    private Config config;
    @Autowired
    private WeChatPayProperties wxPayProperties;

    /**
     * 微信支付
     *
     * @param trading 订单信息
     * @return
     */
    @Override
    public PayResDTO pay(Trading trading) {
        if (PayTypeEnum.NATIVE.getValue().equals(trading.getTradingType())) {
            log.info("微信扫码支付");
            return payNative(trading);
        }
        log.info("其他支付微信支付");

        return null;
    }

    @Override
    public Integer getPayChannel() {
        return PayChannelEnum.WECHAT_PAY.getCode();
    }

    /**
     * 微信扫码支付
     *
     * @param trading 订单信息
     * @return
     */
    public PayResDTO payNative(Trading trading) {
        NativePayService nativePayService = new NativePayService.Builder().config(config).build();

        //构造请求参数
        PrepayRequest prepayRequest = new PrepayRequest();
        Amount amount = new Amount();
        amount.setCurrency("CNY");
        amount.setTotal(trading.getTradingAmount().multiply(new BigDecimal(100)).intValue());
        prepayRequest.setAmount(amount);
        prepayRequest.setAppid(wxPayProperties.getAppid());
        prepayRequest.setMchid(wxPayProperties.getMerchantId());
        prepayRequest.setDescription(trading.getMemo());
        prepayRequest.setNotifyUrl(wxPayProperties.getNotifyUrl());
        prepayRequest.setOutTradeNo(trading.getTradingOrderNo());
        //创建预支付
        PrepayResponse prepay = nativePayService.prepay(prepayRequest);
        log.info("微信扫码支付结果：{}", prepay);

        //构建返回结果
        PayResDTO payResDTO = new PayResDTO();
        payResDTO.setPayOrderNo(trading.getTradingOrderNo());
        //TODO payResDTO.setTransactionId();
        //二维码的Base64数据
        QrConfig qrConfig = new QrConfig();
        qrConfig.setHeight(300);
        qrConfig.setWidth(300);

        String qrCode = QrCodeUtil.generateAsBase64(prepay.getCodeUrl(), qrConfig, IMAGE_TYPE_PNG);
        payResDTO.setQrCode(qrCode);
        return payResDTO;
    }

}
